package com.funny.stock.user.service;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @author pengl
 * @date 2020/7/31
 */
@Component
public class ApplicationConfig implements ApplicationContextAware {

    ApplicationContext applicationContext;

    public boolean isProductEnv(){
        return isEnvironment("product");
    }

    public boolean isDevEnv(){
        return isEnvironment("dev");
    }

    private boolean isEnvironment(String profile){
        String[] profiles = applicationContext.getEnvironment().getActiveProfiles();
        if(profiles == null) return false;

        for (int i = 0; i < profiles.length; i++) {
            if(profiles[i].contains(profile)){
                return true;
            }
        }

        return false;
    }

    @Override
    public void setApplicationContext(ApplicationContext context) throws BeansException {
        applicationContext = context;
    }
}
